#include<iostream>|
#include<queue>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
#define x first
#define y second
const int N = 1e5 + 10, M = 1e6 + 10;
int h[N], e[M], ne[M], w[M], idx;
bool st[N];
LL dis[N];
int n, m;
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dij()
{
	priority_queue<PII, vector<PII>, greater<PII> >q;
	for (int i = 1; i <= n; i++) {
		dis[i] = 1e15;
	}
	dis[1] = 0;
	q.push({ 0,1 });
	while (q.size()) {
		PII t = q.top();
		q.pop();
		int ver = t.y;
		LL distance = t.x;
		if (st[ver])continue;
		st[ver] = 1;
		for (int i = h[ver]; ~i; i = ne[i]) {
			int j = e[i];
			if (!st[j] &&dis[j] > (LL)distance + w[i]) {
				dis[j] = distance + w[i];
				q.push({ dis[j],j });
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	while (m--) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		add(a, b, c);
		add(b, a, c);
	}
	dij();
	cout << dis[n] << endl;
	return 0;
}